package cn.edu.sdju.fengguoyu.ahoedgemonitor;

import javafx.geometry.Bounds;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.io.IOException;
import java.util.List;

public class UserSprite extends Sprite {
    private static Image icon = null;
    String label = "user";
    boolean startAnimate = true;
    public UserSprite() {
        if (icon == null) {
            try {
                icon = new Image(App.class.getResource( "user_icon.png").openStream());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public UserSprite(double x, double y) {
        this();
        this.x = x;
        this.y = y;
        super.width = 24;
        super.height = 46;
    }

    private int frame = 1;
    @Override
    List<String> getHitList() {
        return null;
    }
    private double getR() {
        if (frame >= 292) {
            frame = 0;
        }
        if (frame >= 200) {
            return 100+(frame++-200)*1.1;
        }
        return 100*((1/200.0)*(frame++));
    }
    private double getLineWidth() {
        if (frame >= 292) {
            frame = 0;
        }
        if (frame >= 198) {
            return 100 - ((frame++-200)*1.1);
        }
        return getR();
    }
    private Color getColor() {
        if (frame >= 280) {
            return Color.gray(.5, .0);
        }
        if (frame >= 150) {
            return Color.gray(.5, .1);
        } else {
            return Color.GRAY;
        }
    }
    @Override
    public void draw(GraphicsContext gc) {

        if (startAnimate) {
            gc.setFill(getColor());
            gc.setLineWidth(getLineWidth());
            gc.strokeOval(x-getR()/2.0 +(width/2.0), y -getR()/2.0+(height/2.0), getR(),getR());
        }

        gc.drawImage(icon,x, y, width, height);
        gc.setFont(new Font("Arial", 14));
        gc.setFill(Color.BLUE);
        Text text = new Text(label);
        text.setFont(new Font("Arial", 14));

        Bounds bounds = text.getBoundsInLocal();
        double textWidth = bounds.getWidth();
        gc.fillText(label,x - (textWidth/2) + (width/2), y+height+8);
    }
}
